package com.cs.interceptor;

import com.cs.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //过滤登录和注册的请求
//        String uri = request.getRequestURI();
//        if (uri.indexOf("login.do") >= 0 || uri.indexOf("regist.do") >= 0) {
//            //放行
//            return true;
//        }

        //判断session域中有没有user
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            //放行
            return true;
        } else {
            //调转到登录页面
            response.sendRedirect("/shoplogin.html");
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
